package com.spzx.product;

import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class SpzxProductApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class,args);
        System.out.println("(♥◠‿◠)ﾉﾞ  系统模块启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    @Autowired
    RedissonClient redissonClient;


    @Autowired
    ProductSkuMapper productSkuMapper;

    @Override
    public void run(String... args) throws Exception {
        //初始化布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("sku:bloom:filter"); //由二进制数组和多个hash函数组成
        /*
         * expectedInsertions : 期望的插入数据量
         * falseProbability: 误判率
         * 误判率越低，二进制数组就越大；针对于数据量和可用内存大小来合理设置误判率。
         */
        bloomFilter.tryInit(100,0.01); //创建配置，或更新配置。

        //只是为了测试，演示的代码；
//        List<ProductSku> productSkuList = productSkuMapper.selectList(null);
//        for (ProductSku productSku : productSkuList) {
//            bloomFilter.add(productSku.getId()); //存放数据时，布隆过滤器会使用多个hash函数计算二级制数组索引位置，然后将其标记成1.
//        }
    }
}
